﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kiosk.Library;
using System.IO;
using System.Text;
using System.Drawing;

namespace Kiosk.WebManagement
{
    public partial class EditSlide : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!Page.IsPostBack)
            {
                if (Request["slideID"] == null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("alert('Invalid Slide ID');");
                    sb.Append("window.close();");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "invalidID", sb.ToString(), true);
                    return;
                }

                try
                {
                    Guid slideID = new Guid(Request["slideID"]);
                    if (slideID != null)
                    {
                        Slide s = Slide.GetSlide(slideID);
                        string contents = "";
                        string loc = Server.MapPath("~/upload/" + s.Location);
                        if (!File.Exists(loc))
                        {
                            FileStream f = File.Create(loc);
                            f.Close();
                            f.Dispose();
                        }

                        using (StreamReader sr = new StreamReader(loc))
                        {
                            contents = sr.ReadToEnd();
                        }
                        txtEditor.Value = contents;
                    }
                }
                catch (Exception ex)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("alert('Error retrieving slide');");
                    sb.Append("window.close();");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "error", sb.ToString(), true);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Guid slideID = new Guid(Request["slideID"]);
            try
            {
                if (slideID != null && Slide.Exists(slideID))
                {
                    Slide s = Slide.GetSlide(slideID);
                    string loc = Server.MapPath("~/upload/" + s.Location);


                    using (StreamWriter sw = new StreamWriter(loc, false))
                    {
                        sw.Write(txtEditor.Value);
                        sw.Close();
                    }

                    lblSaveTime.Text = string.Format("Slide saved at {0}", DateTime.Now);
                    lblSaveTime.ForeColor = Color.White;
                }
            }
            catch (Exception ex)
            {
                lblSaveTime.Text = "Error Saving Slide";
                lblSaveTime.ForeColor = Color.Red;
            }
        }
    }
}
